package cn.oa.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import cn.oa.bean.Account;
import cn.oa.bean.Register;
import cn.oa.service.AccountService;

@Controller

public class AccountControler {

	@Resource
	private AccountService ac;

	@PostMapping("/login")
	public String login(ModelMap mmp,Account account,HttpServletRequest req) {
		account = ac.login(account);
		if (account != null) {
			Integer status = account.getStatus();
			if (status == null || account.getStatus() == 0) {
				mmp.addAttribute("errorMessage", "账号已禁用，请联系管理员");
				return "login";
			} else {
				HttpSession session = req.getSession();
				session.setAttribute("Account", account);
				mmp.put("Account", account);
				return "redirect:/";
			}
		} else {
			mmp.addAttribute("errorMessage", "账号密码错误");
			return "login";
		}

	}
   @PostMapping("/insert")
   public String register(ModelMap mmp,Register register) {
	   Boolean bool = ac.register(register);
		if(bool) {
			return "redirect:/";
		}
		else {
			mmp.addAttribute("errorMessage", "添加失败");
			return "register";
		}
   }
   @GetMapping("/clear")
   @ResponseBody
   public String loginout(HttpServletRequest req) {
	   HttpSession session = req.getSession();
	   session.removeAttribute("Account");
	   return "/";
   }
   
}
